using System.IO;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using SQLite;
using Windows.Storage;
using System;
using Windows.Foundation.Collections;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework.Input;
using Alex2.Model;
using Windows.UI.Popups;


namespace Alex2
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Game
    {
        GraphicsDeviceManager _graphics;
        SpriteBatch _spriteBatch;
        SpriteFont word;
        Texture2D bee, antag,bee2;
        
        //temporary list -- for storage only
        List<Place> tlugar;
        List<Character> ttaos;
        List<Item> tgamits;
        List<Relation> trela;

        //list na list -- use this
        List<Place> lugar;
        List<Character> taos;
        List<Item> gamits;
        List<Relation> rela;

        List<Texture2D> taoImage;
        List<Texture2D> lugarImage;
        List<Texture2D> gamitsImage;
        
        int lala = 0,mouseX, mouseY;
        BlankPage1 _page;
        String choc;
        MouseState m;
        Search hanap;
        List<String> inventory;
        Color[][] pixel = new Color[500][];

        InGameGUI igg;
        
        public Game1()
        {
            _graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            _page = new BlankPage1();
        }

        public int getNum(int a)
        {
            int nume;
            if (a == 1)
                nume = 1;
            else
                nume = 2;
            return nume;
        }

        public void setLala(int num){
        lala = num;
        }

        public int getLala()
        {
            return lala;
        }

        private Color[,] TextureTo2DArray(Texture2D texture)
        {
            Color[] colors1D = new Color[texture.Width * texture.Height];
            texture.GetData(colors1D);

            Color[,] colors2D = new Color[texture.Width, texture.Height];
            for (int x = 0; x < texture.Width; x++)
                for (int y = 0; y < texture.Height; y++)
                    colors2D[x, y] = colors1D[x + y * texture.Width];

            return colors2D;
        }
       
        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
            LoadContent();
            mouseX = 0;
            mouseY = 0;
            hanap = new Search();
            
            inventory = new List<string>();
            tlugar = new List<Place>();
            ttaos = new List<Character>();
            tgamits = new List<Item>();
            trela = new List<Relation>();

            lugarImage = new List<Texture2D>();
          

           //hanap.getPath(new Point(0, 0), new Point(4, 5),bee2); //1,0
          // hanap.getPath(new Point(270, 130), new Point(270, 131), bee2);
           //hanap.getPath(new Point(270, 130), new Point(271, 200),bee2);
            
        }
        
       

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
           
            _spriteBatch = new SpriteBatch(GraphicsDevice);
            trela = _page.initializeWold(1); //nasa world na id#1
            tgamits = _page.retrieveItems(trela); //get all the needed items
            ttaos = _page.retrieveCharacter(trela); //get all the needed character
           
            taoImage = new List<Texture2D>();
            gamitsImage = new List<Texture2D>();
            lugar = new List<Place>();
            taos = new List<Character>();
            gamits = new List<Item>();
            rela = new List<Relation>();

            for (int i = 0; i < ttaos.Count; i++)
            {
                taos.Add(ttaos.ElementAt(i));
                taoImage.Add(Content.Load<Texture2D>(ttaos.ElementAt(i).pic));
            }

            for (int i = 0; i < tgamits.Count; i++)
            {
                gamits.Add(tgamits.ElementAt(i));
                gamitsImage.Add(Content.Load<Texture2D>(tgamits.ElementAt(i).pic));
            }
            
            String temAd;
            temAd = _page.getName(2);
            
            antag = Content.Load<Texture2D>(temAd);
            bee2 = Content.Load<Texture2D>("Tracker/background2");
            bee = Content.Load<Texture2D>("beegees/test");
           
            
            word = Content.Load<SpriteFont>("Fonty");
            choc = "";// _page.getName(2);

            igg = new InGameGUI(Content.Load<Texture2D>("GUI/dropdown"), word);
           // hanap.getPath(new Point(270, 130), new Point(270, 131), bee2);
            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // TODO: Add your update logic here
             ProcessMouse();

             igg.Update(gameTime);
            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            _spriteBatch.Begin();

            _spriteBatch.Draw(bee2, new Rectangle(0, 0, 1366, 767), Color.White);
            _spriteBatch.Draw(bee, new Rectangle(0, 0, 1366, 767), Color.White); //1068 602
            _spriteBatch.Draw(antag, new Rectangle(270, 130, 51, 81), Color.White);//270
        
            
            _spriteBatch.DrawString(word, choc, new Vector2(50.0f, 250.0f), Color.White);
            _spriteBatch.DrawString(word, "X:" + m.X.ToString() + "Y:" + m.Y.ToString(), new Vector2(50.0f, 450.0f), Color.White);
           
            for (int d = 0; d < taoImage.Count; d++)
                _spriteBatch.Draw(taoImage.ElementAt(d), new Rectangle(taos.ElementAt(d).xco, taos.ElementAt(d).yco, 51, 81), Color.White);
            

            for (int d = 0; d < gamitsImage.Count; d++)
                _spriteBatch.Draw(gamitsImage.ElementAt(d), new Rectangle(gamits.ElementAt(d).xco, gamits.ElementAt(d).yco, gamits.ElementAt(d).dimX, gamits.ElementAt(d).dimY), Color.White);
            
                //System.Diagnostics.Debug.WriteLine(groundColors[500, 300]);

                //1389 767
                // TODO: Add your drawing code here
            igg.Draw(_spriteBatch);
                base.Draw(gameTime);
            _spriteBatch.End();
        }

        public Boolean isClick(Texture2D ima, int mX, int mY,int cX, int cY)
        {
            int x1, x2, y1, y2;
            Boolean click = false;

            x1 = cX;
            y1 = cY;
            x2 = x1 + ima.Bounds.Width;
            y2 = y1 + ima.Bounds.Height;
          

           // System.Diagnostics.Debug.WriteLine(x1);
         //   System.Diagnostics.Debug.WriteLine(y1);
          //  System.Diagnostics.Debug.WriteLine(x2);
          //  System.Diagnostics.Debug.WriteLine(y2);
            if (mY >= y1 && mY <= y2)
            {
                if (mX >= x1 && mX <= x2)
                {
                    click = true;
                }

                
            }
            return click;
        }

        private void ProcessMouse()
        {
            m = Mouse.GetState();
            if (m.LeftButton == ButtonState.Pressed)
            {
                
                if (isClick(gamitsImage.ElementAt(0), m.X, m.Y, gamits.ElementAt(0).xco, gamits.ElementAt(0).yco))
                {
                    choc = "This is a water.";
                    System.Diagnostics.Debug.WriteLine("Water");
                    //add to inventory
                    inventory.Add("water");
                }

                if (isClick(gamitsImage.ElementAt(1), m.X, m.Y, gamits.ElementAt(1).xco, gamits.ElementAt(1).yco))
                {
                    choc = "This is a radio.";
                    System.Diagnostics.Debug.WriteLine("Radio");
                    inventory.Add("radio");
                }
               //hanap.getPath(new Point(270, 130), new Point(384, 445), bee2);
               //384  445         270,135
                //System.Diagnostics.Debug.WriteLine(hanap);
                
            }
            
       }
     }
}
